package fasilkom.antz;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.OpenGLGameLoader;
import com.golden.gamedev.engine.audio.JavaLayerMp3Renderer;
import com.golden.gamedev.funbox.GameSettings;
import com.golden.gamedev.object.SpriteGroup;

import fasilkom.antz.debug.StageTest;
import fasilkom.antz.util.CustomizedLoader;


/**
 * Kelas utama dalam game ini dan tempat menyimpan variabel yang global dalam
 * game.
 * 
 */
public class Antz extends GameEngine {
	{ distribute = true; }
	
	// PROPERTY //
	
	/** Pemain utama. */
	public GameChar zee;
	
	public SpriteGroup PLAYER_GROUP;
	
	// VARIABLE //
	
	public static final int MAIN_MENU = 4;
	public static final int STAGE_1 = 1;
	public static final int INTRO_STORY = 2;
	public static final int WORLD_MAP = 3;
	public static final int POST_LEVEL = 5;
	public static final int GAME_OVER = 10;
	
	public int antSaved;
	public int money;
	public int healthPotion;
	public int energyPotion;
	public LinkedList<GameChar> dead;
	
	/** Settingan game. */
	private GameSettings settings;
	
	// METHOD //
	
	public static void main(String[] args) {
		Antz game = new Antz();
		OpenGLGameLoader loader = new OpenGLGameLoader();
		loader.setupLWJGL(game, new Dimension(800, 600), false);
		loader.setWindowTitle("ANTZ");
		loader.start();	
	}
	
	@Override
	public GameObject getGame(int gameID) {
		switch (gameID) {
		case MAIN_MENU:
			return new GameMenu(this);
		case STAGE_1:
			return new StageTest(this);
		case GAME_OVER:
			return new GameOver(this);
		case INTRO_STORY:
			return new IntroStory(this);
		case WORLD_MAP:
			return new WorldMap(this);
		case POST_LEVEL:
			return new PostLevel(this);
		}
		
		return new GameMenu(this);
	}

	@Override
	public void initResources() {
		
		// Player group
		
		zee = new GameChar(this, Status.Z, false);
		zee.setLocation(40, 40);
		zee.setControlState(GameChar.CONTROL_ENABLED);
		
		dead = new LinkedList<GameChar>();
		
		PLAYER_GROUP = new SpriteGroup("Character Group");
		PLAYER_GROUP.add(zee);
		
		//member = new ArrayList<GameChar>(8);
		//member.add(zee);
		
		antSaved = 10;
		money = 0;
		healthPotion = 5;
		energyPotion = 0;
		
		// Hide the mouse pointer
		bsInput.setMouseVisible(false);

		// Load font (font TTF yang dibawa game, bukan dari sistem)
		try {
			InputStream stream = bsIO.getStream("assets/ITCKRIST.TTF");
			Font font;
			font = Font.createFont(Font.TRUETYPE_FONT, stream);
			GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
			stream.close();
		} catch (FontFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void initEngine() {
		super.initEngine();
		
		// Gunakan loader yang ini supaya gambarnya halus (pada alpha).
		bsLoader = new CustomizedLoader(bsIO, Color.MAGENTA);
		
		bsMusic.setBaseRenderer(new JavaLayerMp3Renderer());
		bsSound.setBaseRenderer(new JavaLayerMp3Renderer());
	}
	
	// GETTER/SETTER //

	public GameSettings getSettings() {
		return settings;
	}
	
	public void reset(){
		PLAYER_GROUP = new SpriteGroup("Character Group");
		PLAYER_GROUP.add(zee);
		
		while(!dead.isEmpty()){
			dead.peek().setLocation(40, 40);
			dead.peek().status.health = dead.peek().status.maxHealth;
			dead.peek().status.energy = dead.peek().status.maxEnergy;
			
			dead.peek().setActive(true);
			PLAYER_GROUP.add(dead.poll());
		}
	}
}
